package mge
{
  
  public class Timer
  {
    public function Timer(time_ms:int)
    {
      mTimeValue = time_ms;
    }
    
    public function getDelta(ease:Ease = null):Number
    {
      var date:Date = new Date;
      
      if (!mStartDate) return 0;
      
      var elapsed:Number = date.getTime() - mStartDate.getTime();
      
      var delta:Number = elapsed / mTimeValue;
      if (delta > 1.0)
      {
        delta = 1.0;
        if (mLoopEnabled)
        {
          mStartDate = date;
        }
      }
      
      if (ease)
        delta = ease.delta(delta, 1);
      
      return delta;
    }
        
    public function start(loop:Boolean = false):void
    {
      mStartDate = new Date;    
      mLoopEnabled = loop;
    }
    
    public function finished():Boolean
    {
      if (!mStartDate) return false;
      
      var date:Date = new Date;
      var elapsed:Number = date.getTime() - mStartDate.getTime();
      
      if (elapsed >= mTimeValue)
        return true;
      
      return false;
    }
    
    public function stop():void
    {
      // TODO mTmr.stop();
    }
    
    public function setDelta(d:Number):void
    {
      var date:Date = new Date;
      mStartDate.setTime(date.getTime() - mTimeValue * d);
    }
    
    private var mStartDate:Date;
    private var mTimeValue:int;
    private var mLoopEnabled:Boolean;
  }
}